
/*********************************************************/
/*     chaotix_art.cpp     Functions for Chaotix's art   */
/*                         By: Josh Fiorio               */
/*********************************************************/

#include "chaotix_art.h"

extern FILE *ROM;           /* TODO: make functions get passed a file argument */
extern int rom_pos;         /* TODO: somehow get passed this to the funcs. as well */

void InitArt(art &it, long address)
{
	memset(&it, 0, sizeof(it));

	//set the address
	it.address = address;

	//declare vars.
    BYTE x1old = 0;
    BYTE x2old = 0;
    BYTE pixold = 0;
    BYTE theold = 0;
    
	//goto where it points to
    rom_pos = it.address;
	fseek(ROM, rom_pos, 0);

    //get x start
    fread(&it.x1, sizeof(it.x1), 1, ROM);
        rom_pos = rom_pos + sizeof(it.x1);
		fseek(ROM, rom_pos, 0);
        it.x1 = jntohs(it.x1);
        
    //get x end
	fread(&it.x2, sizeof(it.x2), 1, ROM);
        rom_pos = rom_pos + sizeof(it.x2);
		fseek(ROM, rom_pos, 0);
        it.x2 = jntohs(it.x2);
        
    //get y start
	fread(&it.y1, sizeof(it.y1), 1, ROM);
        rom_pos = rom_pos + sizeof(it.y1);
		fseek(ROM, rom_pos, 0);
        it.y1 = jntohs(it.y1);
		it.y1 = it.y1 / 256;

    //get y end
 	fread(&it.y2, sizeof(it.y2), 1, ROM);
        rom_pos = rom_pos + sizeof(it.y2);
		fseek(ROM, rom_pos, 0);
        it.y2 = jntohs(it.y2);
		it.y2 = it.y2 / 256;

    //decalre variables for reading lines
    short lineOn = 0;
    short pixon = 0;
    
    //get lines
    short endyet = 0;
    bool done = false;
    
    while (!done)
	{
    
        //get x start
        fread(&x1old, sizeof(x1old), 1, ROM);
            rom_pos = rom_pos + sizeof(x1old);
			fseek(ROM, rom_pos, 0);
			if (x1old > 128) { it.lines[lineOn].x1 = x1old - 256; } else { it.lines[lineOn].x1 = x1old; }
        
		//get x end
        fread(&x2old, sizeof(x2old), 1, ROM);
            rom_pos = rom_pos + sizeof(x2old);
			fseek(ROM, rom_pos, 0);
            if (x2old > 128) { it.lines[lineOn].x2 = x2old - 256; } else { it.lines[lineOn].x2 = x2old; }

        //get y pos
        fread(&it.lines[lineOn].Y, sizeof(it.lines[lineOn].Y), 1, ROM); 
            rom_pos = rom_pos + sizeof(it.lines[lineOn].Y);
			fseek(ROM, rom_pos, 0);
			it.lines[lineOn].Y = jntohs(it.lines[lineOn].Y);
			it.lines[lineOn].Y = it.lines[lineOn].Y / 256;
            
        //get pixels
		pixon=0;
        for (int pixloop = it.lines[lineOn].x1; pixloop <= it.lines[lineOn].x2 - 1; pixloop++)
		{
            fread(&pixold, sizeof(pixold), 1, ROM);
				rom_pos = rom_pos + sizeof(pixold);
				fseek(ROM, rom_pos, 0);

            if (pixold > 128) { it.lines[lineOn].pixels[pixon] = pixold - 256; } else { it.lines[lineOn].pixels[pixon] = pixold; }
            pixon = pixon + 1;
        }	//end for
        
        fread(&endyet, sizeof(endyet), 1, ROM);
        
        if (endyet == 0)
            done = true;
        else
            fseek(ROM, rom_pos, 0);
        //end if
        
        it.nolines = lineOn;
        lineOn = lineOn + 1;
    }	//end while

}	//end of func.

void WriteArt(art &it, long position = -1)
{
    //declare variables
	if (position==-1) { position=it.address; }
	short ender=0;
    BYTE zero=0;
	BYTE newpix=0;
	int xm=0;
	if ((it.x1%2)==1 || (it.x1%2)==-1) {it.x1=it.x1-1; xm=1;}
	if ((it.x2%2)==1 || (it.x2%2)==-1) {it.x2=it.x2+1;}
	short nx1=jhtons(it.x1);
	short nx2=jhtons(it.x2);

	//goto pos of art
    rom_pos = position;
		fseek(ROM, rom_pos, 0);

    //write x1,x2,y1,y2
	fwrite(&nx1, sizeof(it.x1), 1, ROM);
        rom_pos = rom_pos + sizeof(it.x1);
		fseek(ROM, rom_pos, 0);
    
	fwrite(&nx2, sizeof(it.x2), 1, ROM);
        rom_pos = rom_pos + sizeof(it.x2);
		fseek(ROM, rom_pos, 0);
    
    fwrite(&it.y1, sizeof(it.y1), 1, ROM);
        rom_pos = rom_pos + 1;
		fseek(ROM, rom_pos, 0);

	fwrite(&zero, sizeof(zero), 1, ROM);
        rom_pos = rom_pos + 1;
    	fseek(ROM, rom_pos, 0);
    
    fwrite(&it.y2, sizeof(it.y2), 1, ROM);
        rom_pos = rom_pos + 1;
		fseek(ROM, rom_pos, 0);

	fwrite(&zero, sizeof(zero), 1, ROM);
        rom_pos = rom_pos + 1;
    	fseek(ROM, rom_pos, 0);

    //then write line data
    BYTE newx1 = 0;
    BYTE newx2 = 0;

    for (int lineOn=0; lineOn <= it.nolines; lineOn++)
	{
		it.lines[lineOn].x1 = it.lines[lineOn].x1-xm;
		it.lines[lineOn].x2 = it.lines[lineOn].x2-xm;

        //for odd x2 position, pad a transparent pixel to the end
		if (((it.lines[lineOn].x2) % 2) == 1 || ((it.lines[lineOn].x2) % 2) == -1)
		{
			it.lines[lineOn].x2 = it.lines[lineOn].x2 + 1;
			it.lines[lineOn].pixels[(it.lines[lineOn].x2 - it.lines[lineOn].x1)-1] = 0;
		}	//end if

		//for even x1 position, pad a transparent pixel to the beginning
		if (((it.lines[lineOn].x1) % 2) == 1 || ((it.lines[lineOn].x1) % 2) == -1)
		{
			short temp[101];
			temp[0]=0;
			for (int lp=0; lp<=100; lp++)
				temp[lp+1]=it.lines[lineOn].pixels[lp];
			//end for
			memcpy(it.lines[lineOn].pixels, temp, sizeof(temp));
			it.lines[lineOn].x1 = it.lines[lineOn].x1-1;
		}	//end if

		//write line coords
        fwrite(&it.lines[lineOn].x1, sizeof(it.lines[lineOn].x1), 1, ROM);
            rom_pos = rom_pos + 1;
            fseek(ROM, rom_pos, 0);
		fwrite(&it.lines[lineOn].x2, sizeof(it.lines[lineOn].x2), 1, ROM);
            rom_pos = rom_pos + 1;
            fseek(ROM, rom_pos, 0);
		fwrite(&it.lines[lineOn].Y, sizeof(it.lines[lineOn].Y), 1, ROM);
            rom_pos = rom_pos + 1;
            fseek(ROM, rom_pos, 0);
        fwrite(&zero, sizeof(zero), 1, ROM);
            rom_pos = rom_pos + 1;
            fseek(ROM, rom_pos, 0);
            
        //write line pixels
        for (int pixloop=0; pixloop <= (it.lines[lineOn].x2 - it.lines[lineOn].x1)-1; pixloop++)
		{
            newpix = it.lines[lineOn].pixels[pixloop];
			fwrite(&newpix, sizeof(newpix), 1, ROM);
				rom_pos = rom_pos + 1;
				fseek(ROM, rom_pos, 0);
        }	//end for

	}	//end for
    
	fwrite(&ender, sizeof(ender), 1, ROM);
		rom_pos = rom_pos + sizeof(ender);
		fseek(ROM, rom_pos, 0);
}	//end of func
